import java.util.ArrayList;
import java.util.StringTokenizer;
import java.io.File;
import org.w3c.dom.*;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;

public class Raga {

	String name;
	String author;
	ArrayList<Integer> arohan;
	ArrayList<Integer> avrohan;
	ArrayList<ArrayList<Integer>> all_pakad;
	int vadi;
	int samvadi;
	String time;
	String comment;
	ArrayList<String> scale ;

	public Raga(String filepath) {
		scale = new ArrayList<String>();

		
		scale.add("S-");
		scale.add("r-");
		scale.add("R-");
		scale.add("g-");
		scale.add("G-");
		scale.add("m-");
		scale.add("M-");
		scale.add("P-");
		scale.add("d-");
		scale.add("D-");
		scale.add("n-");
		scale.add("N-");

		scale.add("S");
		scale.add("r");
		scale.add("R");
		scale.add("g");
		scale.add("G");
		scale.add("m");
		scale.add("M");
		scale.add("P");
		scale.add("d");
		scale.add("D");
		scale.add("n");
		scale.add("N");
		
		scale.add("S+");
		scale.add("r+");
		scale.add("R+");
		scale.add("g+");
		scale.add("G+");
		scale.add("m+");
		scale.add("M+");
		scale.add("P+");
		scale.add("d+");
		scale.add("D+");
		scale.add("n+");
		scale.add("N+");
		
		

		
		
		System.out.println(scale);
		try {
			DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
					.newInstance();
			DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
			Document doc = docBuilder.parse(new File(filepath));

			// normalize text representation
			// doc.getDocumentElement().normalize ();
			System.out.println("Root element of the doc is "
					+ doc.getDocumentElement().getNodeName());

			NodeList nodes;
			nodes = doc.getElementsByTagName("name");
			name = nodes.item(0).getTextContent().trim();
			System.out.println("Raga Name: " + name);

			nodes = doc.getElementsByTagName("author");
			author = nodes.item(0).getTextContent().trim();
			System.out.println("Author Name: " + author);

			nodes = doc.getElementsByTagName("avrohan");
			String temp = nodes.item(0).getTextContent().trim();
			System.out.println("Avrohan String: " + temp);
			StringTokenizer st = new StringTokenizer(temp, " ");
			avrohan = new ArrayList<Integer>();
			while (st.hasMoreElements()) {
				String temp2 = (String) st.nextElement().toString();
				int index = scale.indexOf(temp2);
				System.out.println("token: "+temp2+ " "+ (index-12));
				avrohan.add(index-12);
			}
			
			st = new StringTokenizer(temp, " ");
		
			while (st.hasMoreElements()) {
				String temp2 = (String) st.nextElement().toString();
				int index = scale.indexOf(temp2);
				System.out.println("token: "+temp2+ " "+ index);
				avrohan.add(index);
			}
			
			st = new StringTokenizer(temp, " ");
			
			while (st.hasMoreElements()) {
				String temp2 = (String) st.nextElement().toString();
				int index = scale.indexOf(temp2);
				 System.out.println("token: "+temp2+ " "+ (index+12));
				avrohan.add(index+12);
			}
			
			
			System.out.println(avrohan);

			
			nodes = doc.getElementsByTagName("arohan");
			temp = nodes.item(0).getTextContent().trim();
			System.out.println("Arohan String: " + temp);
		 st = new StringTokenizer(temp, " ");
			arohan = new ArrayList<Integer>();
			while (st.hasMoreElements()) {
				String temp2 = (String) st.nextElement().toString();
				int index = scale.indexOf(temp2);
				System.out.println("token: "+temp2+ " "+ (index-12));
				arohan.add(index-12);
			}
			
			st = new StringTokenizer(temp, " ");
		
			while (st.hasMoreElements()) {
				String temp2 = (String) st.nextElement().toString();
				int index = scale.indexOf(temp2);
				System.out.println("token: "+temp2+ " "+ index);
				arohan.add(index);
			}
			
			st = new StringTokenizer(temp, " ");
			
			while (st.hasMoreElements()) {
				String temp2 = (String) st.nextElement().toString();
				int index = scale.indexOf(temp2);
				 System.out.println("token: "+temp2+ " "+ (index+12));
				arohan.add(index+12);
			}
			
			
			System.out.println(arohan);
			
			nodes = doc.getElementsByTagName("vadi");
			temp = nodes.item(0).getTextContent().trim();
			System.out.println("Vadi String: " + temp);
			vadi = scale.indexOf(temp);
			System.out.println("Vadi " + vadi);
			
			nodes = doc.getElementsByTagName("samvadi");
			temp = nodes.item(0).getTextContent().trim();
			System.out.println("Samvadi String: " + temp);
			samvadi = scale.indexOf(temp);
			System.out.println("Samvadi " + samvadi);
			
			//////CODE FOR GETTING THE PAKADS :P ////////
			nodes = doc.getElementsByTagName("p");
			all_pakad= new ArrayList<ArrayList<Integer>>();
			for (int i=0; i<nodes.getLength();i++)
			{
				System.out.println(nodes.item(i).getTextContent().trim());
				
				st = new StringTokenizer(nodes.item(i).getTextContent().trim(), " ");
				ArrayList<Integer> onepakad = new ArrayList<Integer>();
				
				
				while (st.hasMoreElements()) {
					temp = (String) st.nextElement().toString();
					int index = scale.indexOf(temp);
					// System.out.println("token: "+temp+ " "+ index);
					onepakad.add(index);
				}
				all_pakad.add(onepakad);
			}
System.out.println(all_pakad);		
			
nodes = doc.getElementsByTagName("time");
time = nodes.item(0).getTextContent().trim();
System.out.println("Performance Time: " + time);



nodes = doc.getElementsByTagName("comment");
comment = nodes.item(0).getTextContent().trim();
System.out.println("Comments: " + comment);


		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public static void main(String[] args) {

		@SuppressWarnings("unused")
		Raga rag = new Raga("/media/Acad/FinalMajor/Oryx/src/shaymkalyan.xml");
	}

}
